﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.OutterManage.OutterAwardsManage
{
    public partial class awardGro : System.Web.UI.Page
    {
        StudentService studentService = new StudentService();
        ParticipantService participantService = new ParticipantService();
        GroupsService groupsService = new GroupsService();
        ContestService contestService = new ContestService();
        ClassService classService = new ClassService();
        AcademyService academyService = new AcademyService();
        CompetitionService competitionService = new CompetitionService();
        AwardService awardService = new AwardService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AwardsList.DataSource = awardService.GetAllAwards();
                AwardsList.DataTextField = "AWA_NAME";
                AwardsList.DataValueField = "AWA_ID";
                AwardsList.DataBind();

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    string contestId = Request["ContestId"];
                    if (String.IsNullOrEmpty(contestId))
                        return;
                    AddInner(contestId);
                    ConId.Value = contestId;
                }
                string compId = Request["id"];
                if (compId == null)
                    return;
                if (operation == "Modify")
                {
                    ModifyInner(compId);
                }
                else if (operation == "Delete")
                {
                    DeleteInner(compId);
                }
            }
        }

        protected void AddInner(string contestId)
        {
            BoContests contest = contestService.getContest(contestId);
            List<BoContests> contests = new List<BoContests>();
            contests.Add(contest);

            ContestsList.DataSource = contests;
            ContestsList.DataTextField = "CON_NAME";
            ContestsList.DataValueField = "CON_ID";
            ContestsList.DataBind();
        }

        protected void ModifyInner(string parId)
        {
            //BoCompetitions comp = competitionService.getCompetition(compId);
            BoGroups group = groupsService.getGroups(parId);
            ParId.Value = group.GRO_ID;
            teaBox.Text = group.GRO_TEACHER;
            TBcode.Value = group.GRO_NAME;
            TBcode.Attributes["Readonly"] = "readonly";

            AddInner(group.GRO_CONTESTS);

            if (group.GRO_AWARDS != "" && group.GRO_AWARDS != "-1")
                AwardsList.SelectedValue = group.GRO_AWARDS;

            foreach (ListItem item in IsSpecialtyList.Items)
            {
                if (group.GRO_SPECIALTY.ToString() == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }
        }
        protected void DeleteInner(string parId)
        {
            BoGroups group = groupsService.getGroups(parId);
            group.GRO_AWARDS = "-1";
            groupsService.UpdateGroups(group);

            Response.Redirect("OutterAwards.aspx?id=" + group.GRO_CONTESTS);

        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (ParId.Value == "")
            {
                return;
            }

            BoGroups group = groupsService.getGroups(ParId.Value);
            group.GRO_TEACHER = teaBox.Text;
            group.GRO_AWARDS = AwardsList.SelectedValue;
            groupsService.UpdateGroups(group);

            Response.Redirect("OutterAwards.aspx?id=" + group.GRO_CONTESTS);

        }
    }
}